#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/input.h>
#include <stdlib.h>
#include "touch.h"

// 获取一个完整触摸点
TouchPoint get_touch_data(int fd)
{
    struct input_event ev;
    static TouchPoint point = {0};

    point.valid = 0;

    while (read(fd, &ev, sizeof(ev)) == sizeof(ev))
    {
        switch (ev.type)
        {
        case EV_ABS:
            switch (ev.code)
            {
            case ABS_X:
                point.x = ev.value;
                break;
            case ABS_Y:
                point.y = ev.value;
                break;
            case ABS_PRESSURE:
                point.pressure = ev.value;
                break;
            }
            break;

        case EV_KEY:
            if (ev.code == BTN_TOUCH && ev.value == 0)
            {
                // printf("1111111111\n");
                // 触摸抬起时，标记数据有效
                point.valid = 2;   //只有当valid的值为2时，才能判断是触摸抬起，而不是事件中断
                return point;
            }
            break;

        case EV_SYN:
            point.valid = 1;
            return point;
        }
    }

    return (TouchPoint){0}; // 错误或中断返回
}

